package com.winit.datasync.task;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import com.winit.datasync.entity.UpsConfig;
import com.winit.datasync.entity.UpsCustomerMerchSale;
import com.winit.datasync.entity.UpsWarehouse;
import com.winit.datasync.service.UpsConfigService;
import com.winit.datasync.service.UpsCustomerMerchSaleService;
import com.winit.datasync.service.UpsWarehouseService;
import com.winit.datasync.service.MailService;

/**
 * oracle:1 mongodb:2 cassandra:3
 * 
 * @author liangming.deng
 *
 */
@Component
@Lazy(value = false)
public class SyncTask {
	private static Logger LOGGER = LoggerFactory.getLogger(SyncTask.class);

	@Autowired
	private UpsConfigService upsConfigService;
	@Autowired
	private UpsCustomerMerchSaleService upsCustomerMerchSaleService;
	@Autowired
	private UpsWarehouseService upsWarehouseService;
	@Autowired
	private MailService mailService;

	@Value("${sync.type}")
	private String syncType;

	/**
	 * 同步ups数据到oracle 同步syncType配置为1
	 */
	@PostConstruct
	public void syncUpsConfigToOracle() {

		if (StringUtils.isNotBlank(syncType) && "2".equals(syncType)) {
			return;
		}

		LOGGER.info("SyncTask syncUpsConfigToOracle start sync...");
		try {
			// 获取cassandra配置数据
			List<UpsConfig> upsConfigs = upsConfigService.getAll(3);
			// 操作数据到oracle,先删除，在添加
			upsConfigService.batchSave(upsConfigs, 1);
		} catch (Exception e) {
			LOGGER.error("SyncTask syncUpsConfigToOracle error:" + e.getMessage());
			mailService.sendMail(e.getMessage(), "syncUpsConfigToOracle ");
		}

		LOGGER.info("SyncTask syncUpsConfigToOracle end sync...");
	}

	/**
	 * 同步ups数据到mongodb 同步syncType配置为2
	 */
	@PostConstruct
	public void syncUpsConfigToMongodb() {
		if (StringUtils.isNotBlank(syncType) && "1".equals(syncType)) {
			return;
		}
		LOGGER.info("SyncTask syncUpsConfigToMongodb start sync...");
		// mongo事务控制标示
		boolean isTrans = false;
		// ups配置数据
		List<UpsConfig> upsConfigs = new ArrayList<>();
		// mongodb已有数据
		List<UpsConfig> existUpsConfigs = new ArrayList<>();
		try {
			upsConfigs = upsConfigService.getAll(3);
			existUpsConfigs = upsConfigService.getAll(2);
			isTrans = true;

			upsConfigService.batchSave(upsConfigs, 2);
		} catch (Exception e) {
			LOGGER.error("SyncTask syncUpsConfigToMongodb error:" + e.getMessage());
			if (isTrans) {
				// 操作失败,还原原有数据
				try {
					upsConfigService.batchMongoSave(upsConfigs, existUpsConfigs);
				} catch (Exception e1) {
					mailService.sendMail(e.getMessage(), "syncUpsConfigToMongodb ");
					LOGGER.error(
							"SyncTask syncUpsConfigToMongodb transaction error:" + e.getMessage());
				}
			}

			mailService.sendMail(e.getMessage(), "syncUpsConfigToMongodb ");
		}

		LOGGER.info("SyncTask syncUpsConfigToMongodb end sync...");
	}

	/**
	 * 同步UpsCustomerMerchSale数据到oracle 同步syncType配置为1
	 */
	@PostConstruct
	public void syncUpsCustomerMerchSaleToOracle() {

		if (StringUtils.isNotBlank(syncType) && "2".equals(syncType)) {
			return;
		}

		LOGGER.info("SyncTask syncUpsCustomerMerchSaleToOracle start sync...");
		try {
			// 获取cassandra配置数据
			List<UpsCustomerMerchSale> upsCustomerMerchSales = upsCustomerMerchSaleService
					.getAll(3);
			// 操作数据到oracle,先删除，在添加
			upsCustomerMerchSaleService.batchSave(upsCustomerMerchSales, 1);
		} catch (Exception e) {
			LOGGER.error("SyncTask syncUpsCustomerMerchSaleToOracle error:" + e.getMessage());
			mailService.sendMail(e.getMessage(), "syncUpsCustomerMerchSaleToOracle ");
		}

		LOGGER.info("SyncTask syncUpsCustomerMerchSaleToOracle end sync...");
	}

	/**
	 * 同步UpsCustomerMerchSale数据到mongodb 同步syncType配置为2
	 */
	@PostConstruct
	public void syncUpsCustomerMerchSaleToMongodb() {
		if (StringUtils.isNotBlank(syncType) && "1".equals(syncType)) {
			return;
		}
		LOGGER.info("SyncTask syncUpsCustomerMerchSaleToMongodb start sync...");
		// mongo事务控制标示
		boolean isTrans = false;
		// ups配置数据
		List<UpsCustomerMerchSale> upsCustomerMerchSales = new ArrayList<>();
		// mongodb已有数据
		List<UpsCustomerMerchSale> existUpsCustomerMerchSales = new ArrayList<>();
		try {
			upsCustomerMerchSales = upsCustomerMerchSaleService.getAll(3);
			existUpsCustomerMerchSales = upsCustomerMerchSaleService.getAll(2);
			isTrans = true;

			upsCustomerMerchSaleService.batchSave(upsCustomerMerchSales, 2);
		} catch (Exception e) {
			LOGGER.error("SyncTask syncUpsCustomerMerchSaleToMongodb error:" + e.getMessage());
			if (isTrans) {
				// 操作失败,还原原有数据
				try {
					upsCustomerMerchSaleService.batchMongoSave(upsCustomerMerchSales,
							existUpsCustomerMerchSales);
				} catch (Exception e1) {
					mailService.sendMail(e.getMessage(), "syncUpsCustomerMerchSaleToMongodb ");
					LOGGER.error("SyncTask syncUpsCustomerMerchSaleToMongodb transaction error:"
							+ e.getMessage());
				}
			}

			mailService.sendMail(e.getMessage(), "syncUpsCustomerMerchSaleToMongodb ");
		}

		LOGGER.info("SyncTask syncUpsCustomerMerchSaleToMongodb end sync...");
	}

	/**
	 * 同步UpsWarehouse数据到oracle 同步syncType配置为1
	 */
//	@PostConstruct
	public void syncUpsWarehouseToOracle() {

		if (StringUtils.isNotBlank(syncType) && "2".equals(syncType)) {
			return;
		}

		LOGGER.info("SyncTask syncUpsWarehouseToOracle start sync...");
		try {
			// 获取cassandra配置数据
			List<UpsWarehouse> upsWarehouses = upsWarehouseService.getAll(3);
			// 操作数据到oracle,先删除，在添加
			upsWarehouseService.batchSave(upsWarehouses, 1);
		} catch (Exception e) {
			LOGGER.error("SyncTask syncUpsWarehouseToOracle error:" + e.getMessage());
			mailService.sendMail(e.getMessage(), "syncUpsWarehouseToOracle ");
		}

		LOGGER.info("SyncTask syncUpsWarehouseToOracle end sync...");
	}

	/**
	 * 同步UpsWarehouse数据到mongodb 同步syncType配置为2
	 */
	@PostConstruct
	public void syncUpsWarehouseToMongodb() {
		if (StringUtils.isNotBlank(syncType) && "1".equals(syncType)) {
			return;
		}
		LOGGER.info("SyncTask syncUpsWarehouseToMongodb start sync...");
		// mongo事务控制标示
		boolean isTrans = false;
		// ups配置数据
		List<UpsWarehouse> upsWarehouses = new ArrayList<>();
		// mongodb已有数据
		List<UpsWarehouse> existUpsWarehouses = new ArrayList<>();
		try {
			upsWarehouses = upsWarehouseService.getAll(3);
			existUpsWarehouses = upsWarehouseService.getAll(2);
			isTrans = true;

			upsWarehouseService.batchSave(upsWarehouses, 2);
		} catch (Exception e) {
			LOGGER.error("SyncTask syncUpsWarehouseToMongodb error:" + e.getMessage());
			if (isTrans) {
				// 操作失败,还原原有数据
				try {
					upsWarehouseService.batchMongoSave(upsWarehouses, existUpsWarehouses);
				} catch (Exception e1) {
					mailService.sendMail(e.getMessage(), "syncUpsWarehouseToMongodb ");
					LOGGER.error("SyncTask syncUpsWarehouseToMongodb transaction error:"
							+ e.getMessage());
				}
			}

			mailService.sendMail(e.getMessage(), "syncUpsWarehouseToMongodb ");
		}

		LOGGER.info("SyncTask syncUpsWarehouseToMongodb end sync...");
	}

}
